﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XcoAppSpaces.Core;
using Microsoft.Ccr.Core;

namespace XcoAppSpaces.Core
{
	/// <summary>
	/// Represents a timed action that is executed with a ccr timer port.
	/// Call Invoke() or InvokeDelayed() to start the timed action.
	/// </summary>
	public class XcoTimedAction : IDisposable
	{
		/// <summary>
		/// Continuation action for the timed action.
		/// </summary>
		public enum Continuation
		{
			/// <summary>
			/// The action should be rescheduled and executed again after the timed interval.
			/// </summary>
			Reschedule,

			/// <summary>
			/// The action should not be executed again, the timer should be stopped.
			/// </summary>
			Stop
		}

		private readonly DispatcherQueue dpq;
		private readonly Func<Continuation> action;
		private readonly TimeSpan interval;

		private readonly Port<DateTime> timerPort = new Port<DateTime>();
		bool running = true;

		/// <summary>
		/// Creates a new XcoTimedAction.
		/// </summary>
		/// <param name="dpq">The dispatcher queue that should be used for the timer (the queue's EnqueueTimer method is used).</param>
		/// <param name="interval">The timer interval.</param>
		/// <param name="action">The action that should be executed.</param>
		public XcoTimedAction(DispatcherQueue dpq, TimeSpan interval, Func<Continuation> action)
		{
			this.dpq = dpq;
			this.interval = interval;
			this.action = action;

			timerPort = dpq.Receive<DateTime>(_ => InvokeAction());
		}

		private void InvokeAction()
		{
			if (!running)
				return;
			var continuationAction = action();
			if (continuationAction == Continuation.Reschedule)
				EnqueueTimer();
		}

		private void EnqueueTimer()
		{
			dpq.EnqueueTimer(interval, timerPort);
		}

		/// <summary>
		/// Asynchronously starts the timer without delay.
		/// </summary>
		public void Invoke()
		{
			if (!running)
				throw new ObjectDisposedException("SpaceTimedAction");
			timerPort.Post(DateTime.Now);
		}

		/// <summary>
		/// Asynchonously starts the timer, using the predefined timer interval as delay.
		/// </summary>
		public void InvokeDelayed()
		{
			if (!running)
				throw new ObjectDisposedException("SpaceTimedAction");
			EnqueueTimer();
		}

		/// <summary>
		/// Disposes the XcoTimedAction.
		/// </summary>
		public void Dispose()
		{
			running = false;
		}
	}
}
